<?php

class ChapterController extends Controller
{
    public $layout='column2';  
    public function actionIndex()
    {
        $key=  empty($_POST['key'])?'':$_POST['key'];
        if(empty($key)){
            $sql="select * from chapter where state=1 order by sequence asc";
            $chapter=  Chapter::model()->findAllBySql($sql);
        }  else {
//            print_r($key);die;
            $sql="select * from chapter where state=1 and name like '%".$key."%' order by sequence asc";
            $chapter=  Chapter::model()->findAllBySql($sql);
        }
        
        $this->render('index',array('model'=>$chapter,'key'=>$key));
    }
    public function actionCreate(){
        $model = new Chapter();
        if(isset($_POST['Chapter'])){
            $model->attributes=$_POST['Chapter'];
            if($model->validate() && $model->save()){
                $this->redirect($this->createUrl('/chapter'));
            } 
        }
        $this->render('_form',array('model'=>$model));
    }

    public function actionUpdate(){
        $id=$_GET['id'];
        $model =  Chapter::model()->findByPk($id);
        if(isset($_POST['Chapter'])){
            $model->attributes=$_POST['Chapter'];
            if($model->validate() && $model->save()){
                $this->redirect($this->createUrl('/chapter'));
            } 
        }
        $this->render('_form',array('model'=>$model));
    }
    // Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}